package cn.amaake.controller;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

import cn.amaake.common.AmaakePage;
import cn.amaake.common.Result;
import cn.amaake.service.ClassService;
import cn.amaake.service.ClassServiceImp;
import cn.amaake.common.model.Class;
import cn.amaake.common.model.ClassCourse;
import cn.amaake.common.model.Course;
import cn.amaake.common.model.User;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ClassController extends Controller {

    @Inject(ClassServiceImp.class)
    private ClassService classService;

    public void index(){
        render("/views/class/index.html");
    }

    public void getFuClassList(){
        List<Class> fuClassList = classService.getFuClassList();
        Result result = new Result();
        result.setData(fuClassList);
        renderJson(result);
    }

    public void showAddFuClass(){
        render("/views/class/addFuClass.html");
    }

    public void addFuClass(){
        String name = getPara("name");
        User user = getSessionAttr("usersession");
        Class aClass = new Class();
        aClass.setClassName(name);
        aClass.setUserId(user.getID());
        aClass.setClassId(0);
        aClass.setCreatetime(new Date());
        boolean boo = aClass.save();
        Result result = new Result();
        if(boo){
            result.setErrorNo(0);
            result.setErrorInfo("添加成功！");
        }else{
            result.setErrorNo(500);
            result.setErrorInfo("添加失败！");
        }
        renderJson(result);
    }

    public void showEditFuClass(){
        String menuId = getPara("menuId");
        setAttr("id",menuId);
        render("/views/class/editFuClass.html");
    }

    public void getFuClass(){
        String id = getPara("menuId");
        Class aClass = Class.dao.findById(id);
        Result result = new Result();
        result.setData(aClass);
        renderJson(result);
    }

    public void editFuClass(){
        String id = getPara("id");
        String name = getPara("class_name");
        Result result = new Result();
        Class aClass = Class.dao.findById(id);
        aClass.setClassName(name);
        boolean boo = aClass.update();
        if (boo) {
            result.setErrorNo(0);
            result.setErrorInfo("更新成功！");
        } else {
            result.setErrorNo(500);
            result.setErrorInfo("更新失败！");
        }
        renderJson(result);
    }

    public void removeFuClass(){
        String menuId = getPara("menuId");
        Result result = new Result();
        boolean boo = Class.dao.deleteById(menuId);
        if(boo){
            result.setErrorNo(0);
            result.setErrorInfo("删除成功！");
        }else{
            result.setErrorNo(500);
            result.setErrorInfo("删除失败！");
        }
        renderJson(result);
    }

    public void getClassPage(){
        int pageNumber = getParaToInt("pageNumber");
        int pageSize = getParaToInt("pageSize");
        String menuId = getPara("menuId");
        Page<Class> classList = classService.getClassPage(pageNumber,pageSize,menuId);
        Result result = new Result();
        result.setData(new AmaakePage(classList));
        renderJson(result);
    }

    public void showAddClass(){
        setAttr("id",getPara("menuId"));
        render("/views/class/add.html");
    }

    public void addClass(){
        int id = getParaToInt("id");
        String class_name = getPara("class_name");
        String major = getPara("major");
        String teacher = getPara("teacher");
        String schoolsystem = getPara("schoolsystem");
        User user = getSessionAttr("usersession");
        Class aclass = new Class();
        aclass.setClassName(class_name);
        aclass.setMajor(major);
        aclass.setTeacher(teacher);
        aclass.setSchoolsystem(schoolsystem);
        aclass.setUserId(user.getID());
        aclass.setClassId(id);
        aclass.setCreatetime(new Date());
        boolean boo = aclass.save();
        Result result = new Result();
        if(boo){
            result.setErrorNo(0);
            result.setErrorInfo("添加成功！");
        }else{
            result.setErrorNo(500);
            result.setErrorInfo("添加失败！");
        }
        renderJson(result);
    }

    public void removeClass(){
        String id = getPara("id");
        boolean boo = Class.dao.deleteById(id);
        Result result = new Result();
        if(boo){
            result.setErrorNo(0);
            result.setErrorInfo("删除成功！");
        }else{
            result.setErrorNo(500);
            result.setErrorInfo("删除失败！");
        }
        renderJson(result);
    }

    public void showEditClass(){
        setAttr("id",getPara("id"));
        render("/views/class/edit.html");
    }

    public void getClassa(){
        String id = getPara("id");
        Class aClass = Class.dao.findById(id);
        Result result = new Result();
        result.setData(aClass);
        renderJson(result);
    }

    public void editClass(){
        String id = getPara("id");
        String name = getPara("class_name");
        String major = getPara("major");
        String teacher = getPara("teacher");
        String schoolsystem = getPara("schoolsystem");
        Result result = new Result();
        Class aClass = Class.dao.findById(id);
        aClass.setClassName(name);
        aClass.setMajor(major);
        aClass.setTeacher(teacher);
        aClass.setSchoolsystem(schoolsystem);
        boolean boo = aClass.update();
        if (boo) {
            result.setErrorNo(0);
            result.setErrorInfo("更新成功！");
        } else {
            result.setErrorNo(500);
            result.setErrorInfo("更新失败！");
        }
        renderJson(result);
    }
    
    public void showSetUpCourse() {
    	String id = getPara("id");
    	Class aClass = Class.dao.findById(id);
    	List<Course> courseList = Course.dao.getCourseList();
    	ClassCourse classCourse = ClassCourse.dao.findById(id);
    	if(classCourse!=null) {
    		String[] courseids = classCourse.getCourseids().split(",");
        	List<Course> edCourses = new ArrayList<Course> ();
        	for(Course course : courseList){
                for(String courseid : courseids){
                    if(course.getId() == Integer.parseInt(courseid)){
                    	course.put("selected","selected=\"selected\"");
                    }
                }
                edCourses.add(course);
            }
    	}
    	setAttr("id", id);
    	setAttr("aclass", aClass);
    	setAttr("courseList", courseList);
    	render("/views/class/showSetUpCourse.html");
    }
    
    public void setUpCourse() {
    	String id = getPara("id");
    	String courseids = getPara("courseids");
    	ClassCourse classCourse = ClassCourse.dao.findById(id);
    	boolean boo = false;
    	if(classCourse ==null) {
    		classCourse = new ClassCourse();
    		classCourse.setClassId(Integer.parseInt(id));
        	classCourse.setCourseids(courseids);
        	boo = classCourse.save();
    	}else {
    		classCourse.setClassId(Integer.parseInt(id));
        	classCourse.setCourseids(courseids);
    		boo = classCourse.update();
    	}
    	Result result = new Result();
        if (boo) {
            result.setErrorNo(0);
            result.setErrorInfo("设置成功！");
        } else {
            result.setErrorNo(500);
            result.setErrorInfo("设置失败！");
        }
        renderJson(result);
    }

}
